package top.qitun.controller;

import java.util.Date;
import java.util.Optional;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import top.qitun.bizz.catalog.catalog.entity.Catalog;
import top.qitun.bizz.catalog.catalog.entity.CatalogBO;
import top.qitun.bizz.catalog.catalog.service.CatalogService;
import top.qitun.modal.request.CatalogCreateRequest;
import top.qitun.model.response.ObjectCollectionResponse;
import top.qitun.model.response.ObjectDataResponse;
import top.qitun.util.OrikaMapper;

/**
 * 店铺创建类
 * 
 * @author Cloudwei
 *
 */
@RestController
@RequestMapping(value = "/v1")
public class CatalogController {
	Logger logger = LoggerFactory.getLogger(getClass());

	@Autowired
	private CatalogService catalogService;
    
	@ApiOperation(value = "创建条目", notes = "创建条目")
	@RequestMapping(value = "/catalog", method = RequestMethod.POST)
	public ObjectDataResponse<Catalog> create(@RequestBody CatalogCreateRequest request) {
		String desc = "创建条目";
		try {
			CatalogBO catalog = null;
			Date now = new Date();
			catalog = OrikaMapper.map(request, CatalogBO.class);
			catalog.setCreateTime(now);
			catalog.setLastUpdateTime(now);
			catalogService.add(catalog);
			return new ObjectDataResponse<Catalog>(0, desc + "成功", catalog);
		} catch (Exception e) {
			logger.error(desc + "失败", e);
			return new ObjectDataResponse<>(-1, desc + "失败.请联系客服" + e.getMessage());
		}
	}

	@ApiOperation(value = "显示店铺菜单种类", notes = "显示店铺菜单种类")
	@RequestMapping(value = "/catalog/shop/{shopid}", method = RequestMethod.GET)
	public ObjectCollectionResponse<CatalogBO> catalogsListInShop(@ApiParam(value = "店铺Id", example = "4",defaultValue="4") @PathVariable(value = "shopid") Integer shopId) {
		String desc = "显示店铺菜单种类";
		try {
			return Optional.ofNullable(catalogService.queryCatalogsListInShop(shopId))
					.map(cataLogList -> new ObjectCollectionResponse<CatalogBO>(1, desc + "成功", cataLogList))
					.orElse(new ObjectCollectionResponse<CatalogBO>(-1, desc + "无数据"));
		} catch (Exception e) {
			logger.error(desc + "失败", e);
			return new ObjectCollectionResponse<CatalogBO>(-1, desc + "失败.请联系客服" + e.getMessage());
		}
	}
	
//	@ApiOperation(value = "根据tableId查询菜单", notes = "根据tableId查询菜单")
//	@RequestMapping(value = "/catalog/table/{id}", method = RequestMethod.GET)
//	public ObjectCollectionResponse<CatalogBO> productListByTable(
//			@ApiParam(example = "2", defaultValue = "2", value = "餐桌Id") @PathVariable(value = "id") Integer tableId) {
//		String desc = "查询分类下菜单";
//		logger.debug(desc + " start");
//		try {
//			ObjectDataResponse<TTableBO> objectDataResponse=shopServiceAPI.queryTableById(tableId);
//			// 根据tableId,查出店铺Id,根据店铺id查出该店铺的菜品
//			return Optional
//					.ofNullable(objectDataResponse.getData())
//					.map(response -> response.getShopId())
//					.map(shopId -> catalogService.queryCatalogsListInShop(shopId))
//					.map(cataLogList -> new ObjectCollectionResponse<CatalogBO>(1, "成功", cataLogList))
//					.orElse(new ObjectCollectionResponse<>(objectDataResponse.getCode(), objectDataResponse.getMessage()));
//			
//			
//		} catch (Exception e) {
//			logger.error(desc + "失败", e);
//			return new ObjectCollectionResponse<>(-1, desc + "失败.请联系客服" + e.getMessage());
//		}
//	}

}
